package DAO;

import Entities.BaseEntity;
import Exceptions.InvalidEntityException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author 647_2
 *
 * Декоратор для класса Accessor, представляет упрощённый доступ к данным.
 */
public class SimpleAccessor {

    private Accessor accessor;

    public SimpleAccessor(Class entity) throws FileNotFoundException, IOException{
        accessor = new Accessor(entity);
        accessor.Initialize();
    }

    public SimpleAccessor(BaseEntity entity) throws FileNotFoundException, IOException{
        accessor = new Accessor(entity);
        accessor.Initialize();
    }

    public <T extends BaseEntity> LinkedList<T> select(T selector, int start, int count) throws InvalidEntityException, ClassNotFoundException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, UnsupportedEncodingException, NoSuchFieldException, FileNotFoundException, IOException{
        System.out.println("Запуск селектора 2");

        return accessor.select(selector, new Long(start), new Long(count));
    }

    public <T extends BaseEntity> LinkedList<T> select(T selector) throws InvalidEntityException, ClassNotFoundException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException, UnsupportedEncodingException, NoSuchFieldException, FileNotFoundException, IOException{
        System.out.println("Запуск селектора 3");

        return this.select(selector, 0, 0);
    }

    public <T extends BaseEntity> boolean delete(T deleter) throws InvalidEntityException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
        System.out.println("Запуск удалятора 2");

        return accessor.delete(deleter);
    }

    public <T extends BaseEntity> boolean update(T updater, T newval) throws InvalidEntityException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException, ClassNotFoundException {
        System.out.println("Запуск обновлятора 2");

        return accessor.update(updater, newval);
    }

    public <T extends BaseEntity> boolean insert(T inserter) throws InvalidEntityException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException {
        System.out.println("Запуск добавлятора 2");

        return accessor.insert(inserter);
    }
}
